<template>
  <div>
    <basic-modal :modalOpt="modal" :footer="null" @closeModal="closeModal">
      <template>
        <basic-form
          :formInfos="s_FormInfo"
          :formDatas="s_formDatas"
          :rules="s_rules"
          @onSubmit="onSubmit"
          @onChangeType="onChangeType"
          @closeModal="closeModal"
        ></basic-form>
      </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import basicForm from '../common/basicForm'
import {getDetailByPid} from '@/views/app/biz/regisLicense/regisLicense'
import {
  getInfoByPid,
  saveInfo
} from './invoice.js'

const s_FormInfo = [
  { label: '结算单号', name: 'settlementPid', type: 'settlementSelect', placeholder: '请选择结算单' },
  { label: '关联销售订单', name: 'saleOrderNo', type: 'input', placeholder: '请输入关联销售订单' },
  { label: '买方单位', name: 'customerName', type: 'input', placeholder: '请输入买方单位' },
  { label: '买方税号', name: 'customerTaxNo', type: 'input', placeholder: '请输入买方税号' },
  { label: '买方地址电话', name: 'customerAddrTell', type: 'input', placeholder: '请输入买方地址电话' },
  { label: '买方银行账号', name: 'customerAccountNum', type: 'input', placeholder: '请输入买方银行账号' },
  { label: '开具单位', name: 'supplierOrgName', type: 'input', placeholder: '请输入开具单位' },
  { label: '开具单位税号', name: 'supplierTaxNo', type: 'input', placeholder: '请输入开具单位税号' },
  { label: '开具单位地址电话', name: 'supplierAddrTel', type: 'input', placeholder: '请输入开具单位地址电话' },
  { label: '开具单位银行账号', name: 'supplierAccountNum', type: 'input', placeholder: '请输入开具单位银行账号' },
  { label: '发票种类', name: 'invoiceType', type: 'dicselect',dicName:'invoiceClass', placeholder: '请输入发票种类' },
  { label: '发票号码', name: 'invoiceNo', type: 'input', placeholder: '请输入发票号码' },
  { label: '发票代码', name: 'invoiceCode', type: 'input', placeholder: '请输入发票代码' },
  { label: '红票标志', name: 'redFlag', type: 'input', placeholder: '请输入红票标志' },
  { label: '无税总价', name: 'totalIunitprice', type: 'input', placeholder: '请输入无税总价' },
  { label: '含税总价', name: 'totalPrice', type: 'input', placeholder: '请输入含税总价' },
  { label: '税率', name: 'tax', type: 'input', placeholder: '请输入税率' },
  { label: '税额', name: 'taxes', type: 'input', placeholder: '请输入税额' },
  { label: ['保存', '重置'], type: 'botton' }
]
const s_formDatas = {
  sysSeq: null,
  pid: null,
  saleOrderNo: null,
  purOrderNo: null,
  customerOrgPid: null,
  settlementPid: null,
  settlementNo: null,
  customerName: null,
  customerTaxNo: null,
  customerAddrTell: null,
  customerAccountNum: null,
  supplierOrgName: null,
  supplierTaxNo: null,
  supplierAddrTel: null,
  supplierAccountNum: null,
  invoiceType: null,
  invoiceNo: null,
  invoiceCode: null,
  redFlag: null,
  totalIunitprice: null,
  totalPrice: null,
  tax: null,
  taxes: null,
  invoiceState: null
}
const s_rules = {
  invoiceType_: [{ required: true, message: '发票类型必填', trigger: 'blur' }],
  supplierName: [{ required: true, message: '供应商名称必选', trigger: 'blur' }],
  totalIunitprice: [{ required: true, message: '结算金额无税总价必填', trigger: 'change' }],
  totalPrice: [{ required: true, message: '结算金额含税总价必填', trigger: 'change' }],
}
export default {
  components: {
    basicModal,
    basicForm
  },
  data() {
    return {
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '新增结算单',
        modalWidth: '1000px',
      },
      s_FormInfo: s_FormInfo,
      s_formDatas: s_formDatas,
      s_rules: s_rules,
      
    }
  },
  computed: {},
  mounted() {
  },
  methods: {
    // 新增按钮 点击了新增
    addModal() {
      for (let i in this.s_formDatas) {
        this.s_formDatas[i]=null
      }
      this.s_formDatas = Object.assign( this.s_formDatas,  {
        sysSeq: 0,
        pid: '',
        invoiceType: '蓝字发票',
      })
      console.log(s_formDatas);
      console.log('点击了新增')
      this.modal.modalVisible = true
      this.modal.modalTitle = "新增发票"
    },
    // 新增按钮 点击了新增
    editModal(val) {
      console.log('点击了编辑')
      this.s_FormInfo=[
        { label: '结算单号', name: 'settlementPid', type: 'settlementSelect', placeholder: '请选择结算单' },
        { label: '关联销售订单', name: 'saleOrderNo', type: 'input', placeholder: '请输入关联销售订单' },
        { label: '买方单位', name: 'customerName', type: 'input', placeholder: '请输入买方单位' },
        { label: '买方税号', name: 'customerTaxNo', type: 'input', placeholder: '请输入买方税号' },
        { label: '买方地址电话', name: 'customerAddrTell', type: 'input', placeholder: '请输入买方地址电话' },
        { label: '买方银行账号', name: 'customerAccountNum', type: 'input', placeholder: '请输入买方银行账号' },
        { label: '开具单位', name: 'supplierOrgName', type: 'input', placeholder: '请输入开具单位' },
        { label: '开具单位税号', name: 'supplierTaxNo', type: 'input', placeholder: '请输入开具单位税号' },
        { label: '开具单位地址电话', name: 'supplierAddrTel', type: 'input', placeholder: '请输入开具单位地址电话' },
        { label: '开具单位银行账号', name: 'supplierAccountNum', type: 'input', placeholder: '请输入开具单位银行账号' },
        { label: '发票种类', name: 'invoiceType', type: 'dicselect',dicName:'invoiceClass', placeholder: '请输入发票种类' },
        { label: '发票号码', name: 'invoiceNo', type: 'input', placeholder: '请输入发票号码' },
        { label: '发票代码', name: 'invoiceCode', type: 'input', placeholder: '请输入发票代码' },
        { label: '红票标志', name: 'redFlag', type: 'input', placeholder: '请输入红票标志' },
        { label: '无税总价', name: 'totalIunitprice', type: 'input', placeholder: '请输入无税总价' },
        { label: '含税总价', name: 'totalPrice', type: 'input', placeholder: '请输入含税总价' },
        { label: '税率', name: 'tax', type: 'input', placeholder: '请输入税率' },
        { label: '税额', name: 'taxes', type: 'input', placeholder: '请输入税额' },
        { label: ['保存', '重置'], type: 'botton' }
      ]
      for (let i in this.s_formDatas) {
        this.s_formDatas[i]=null
      }
      this.getSBypid(val)
      this.modal.modalVisible = true
      this.modal.modalTitle = "修改发票"
    },
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    // 保存成功关闭模态详细框
    closeModal2() {
      this.modal.modalVisible = false
      this.$emit('ok')
    },
    // 模态详细框提交按钮
    onSubmit(val) {
      this.saveObjInfo(val)
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.loading = true
      // this.$message.loading({ content: '提交中'， })
      let res = await saveInfo(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功',  duration: 2 })
        this.closeModal2()
      } else {
        this.$message.error({ content: '提交失败',  duration: 2 })
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        if (res.data) {
          console.log(s_formDatas);
          this.s_formDatas = Object.assign( this.s_formDatas, res.data )
        } else {
          this.addModal()
        }
      }
      console.log(res)
    },
    // 表单编号
    onChangeType(name, val) {
      console.log('formName',name)
      console.log('val',val)
      switch (name) {
        case 'settlementPid': 
          this.setDetail(val[1])
        break
      }
    },
    // 动态设置详细信息
    setDetail (res) {
      console.log(res);
      this.s_formDatas = Object.assign( this.s_formDatas,  {
        customerOrgPid: res.orgPid,
        settlementPid: res.pid,
        settlementNo: res.settlementNo,
        tax:res.tax,
        taxes:res.taxes,
        totalIunitprice:res.totalIunitprice,
        totalPrice:res.totalPrice
      })
    },
    //
    restData (val) {
       this.getSBypid(this.s_formDatas.pid)
    }
  },
}
</script>

<style>
</style>